<?php
/**
 * Created by PhpStorm.
 * User: Tommy
 * Date: 2018/11/13
 * Time: 11:28
 */

namespace App\Services;

use App\Models\Hall;
use App\Models\HallConfig;
use common\enum\MasterLogEnum;
use common\utils\SysCode;

class MonthlyService
{
    const MONTHLY = 0;//最低金额

    /**
     * 修改保底费
     * @param int $id
     * @param $money
     * @param int $type
     * @return mixed
     */
    public static function updateCote(int $id, $money, $type=null)
    {
        if($type){
            // todo 预留功能  设置成“开启线路费”
            $result = 'true';
        }else{
            $result = HallConfig::where(['hall_id' => $id])->update(['fee_monthly_amount' => $money]);
        }
        if (!$result) {
            error_exit(SysCode::FAILED);
        }
        $hallName = Hall::where(['id' => $id])->first()->toArray();
        if($type) {
            $remark = str_replace('{:name}', $hallName['company_name'], MasterLogEnum::REMARK['hall_monthly_setting_line']);
        }else{
            $remark = str_replace(['{:name}','{:amount}'], [$hallName['company_name'],  amount_format($money)],  MasterLogEnum::REMARK['hall_monthly_setting_keep']);
        }
        MasterLogService::writeMasterLog(MasterLogEnum::HALL_MONTHLY_SETTING, MasterLogEnum::SUCCESS, $remark);
        return $result;
    }

    public static function getMonthly(int $id)
    {
        $monthly = HallConfig::where(['hall_id' => $id])->value('fee_monthly_amount');
        if (!empty($monthly) || $monthly > self::MONTHLY) {
            $result = amount_format($monthly, false);
        }
        return $result;
    }
}